/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Base for creating a custom VGUI class; remove the functions you don't use.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

include( "shared.lua" )

local PLAYER = FindMetaTable( "Player" )

function PLAYER:RANK()
	return self:GetNetworkedString( "rank" )
end

PLAYER = nil

local CONSOLE = FindMetaTable( "Entity" )

function CONSOLE:RANK()
	return "Owner"
end

function CONSOLE:Name()
	return "Console"
end

CONSOLE = nil


local PANEL = {}
TimesT = {}
INFOPANEL = nil
INAME = nil

surface.CreateFont( "coolvetica", 22, 500, true, false, "BoardPlayerNameBig" )

function PANEL:Init()
	//Create controls here
	
	surface.CreateFont ("CloseCaption_BoldItalic", 30, 400, true, true, "SNF")
	
	self:SetTitle( " " )
	self:ShowCloseButton( false )
	
	local TopPanel = vgui.Create( "DPanel", self )
		self.TopPanel = TopPanel
		
		
	local Logo = vgui.Create( "DImage", TopPanel )
		Logo:SetImage( "gui/POLogo" )
		self.Logo = Logo
		
	local SName = vgui.Create( "DLabel", TopPanel )
		SName:SetFont( "SNF" )
		SName:SetText( ServerName )
		self.SName = SName
		
		
	local InfoPanel = vgui.Create( "DPanel", self )
		self.InfoPanel = InfoPanel
		
	local NLabel = vgui.Create( "DLabel", InfoPanel )
		NLabel:SetTextColor( color_black )
		NLabel:SetText( "Name" )
		self.NLabel = NLabel
		
	local RLabel = vgui.Create( "DLabel", InfoPanel )
		RLabel:SetTextColor( color_black )
		RLabel:SetText( "Rank" )
		self.RLabel = RLabel
		
	local TLabel = vgui.Create( "DLabel", InfoPanel )
		TLabel:SetTextColor( color_black )
		TLabel:SetText( "Time" )
		self.TLabel = TLabel
		
	local FLabel = vgui.Create( "DLabel", InfoPanel )
		FLabel:SetTextColor( color_black )
		FLabel:SetText( "Frags" )
		self.FLabel = FLabel
		
	local PLabel = vgui.Create( "DLabel", InfoPanel )
		PLabel:SetTextColor( color_black )
		PLabel:SetText( "Ping" )
		self.PLabel = PLabel
		
		
	local PlayerList = vgui.Create( "DPanelList", self )
		PlayerList:EnableVerticalScrollbar( true )
		PlayerList:EnableHorizontal( false )
		PlayerList:SetSpacing( 2 )
		PlayerList:SetPadding( 0.5 )
		self.PlayerList = PlayerList
		
	self.PlyTable = {}
		
	for k,v in pairs( player.GetAll() ) do
		self:AddPlayers( v, k )
	end
	
	self:MakePopup()
	self:Center()

end

function PANEL:AddPlayers( ply, num )

	local PP = {}

		local PPanel = vgui.Create( "DPanel" )
		local PCOLOR = ColorTable[ply:RANK()]
		local TColor = Color( 255, 255, 255, 255 )
		if PCOLOR and math.Max(PCOLOR.r, PCOLOR.g, PCOLOR.b) >= 200 then
			TColor = Color( 0, 0, 0, 255 )
		end
		
			PPanel:SetSize( self.PlayerList:GetWide(), 30 )
			function PPanel:Paint()
				draw.RoundedBox( 2, 0, 0, self:GetWide(), self:GetTall(), color_black )
				draw.RoundedBox( 4, 2, 2, self:GetWide() - 4, self:GetTall() - 4, PCOLOR )
			end
			for rn,r in pairs( RankTable ) do
				if ply:RANK() == r then PPanel.OrderNum = table.getn( RankTable ) - rn break end
			end
			
		local NAME = vgui.Create( "DLabel", PPanel )
			NAME:SetTextColor( TColor )
			NAME:SetText( ply:Nick() )
			NAME:SizeToContents()
			NAME:Center()
			
		local RNK = vgui.Create( "DLabel", PPanel )
		local RX,RY = self.RLabel:GetPos()
			RNK:SetTextColor( TColor )
			RNK:SetText( ply:RANK() )
			RNK:SizeToContents()
			RNK:Center()
			
		local TIME = vgui.Create( "DLabel", PPanel )
		local TX,TY = self.TLabel:GetPos()
			TIME:SetTextColor( TColor )
			TIME:Center()
			function TIME:Think()
				if ply:IsValid() and ply:IsPlayer() and tonumber(TimesT[num]) != nil and tonumber(TimesT[num]) > 0  then
					local TI = tonumber(TimesT[num])
					local TH = math.floor(TI / 3600)
					TIME:SetText( TH .. ":" .. os.date("%M:%S", TI ) )
					TIME:SizeToContents()
				end
			end
			
		local FRAGS = vgui.Create( "DLabel", PPanel )
		local FX,FY = self.FLabel:GetPos()
			FRAGS:SetTextColor( TColor )
			FRAGS:SetText( ply:Frags() )
			FRAGS:SizeToContents()
			FRAGS:Center()
			
		local PING = vgui.Create( "DLabel", PPanel )
		local PX,PY = self.PLabel:GetPos()
			PING:SetTextColor( TColor )
			PING:SetText( ply:Ping() )
			PING:SizeToContents()
			PING:Center()
			
			
		PP.PPanel = PPanel
		PP.NAME = NAME
		PP.RNK = RNK
		PP.TIME = TIME
		PP.FRAGS = FRAGS
		PP.PING = PING
			
		table.insert( self.PlyTable, PP )
		self.PlayerList:AddItem( PPanel )
		self.PlayerList:SortByMember( "OrderNum", true )

end

function PANEL:PerformLayout()
	
	// Let's make sure we perform the layout of the class this panel is based on here as well.
	self.BaseClass.PerformLayout( self )
	
	self.TopPanel:SetPos( 6, 6 )
	self.TopPanel:SetSize( self:GetWide() - 12, self:GetTall() * 0.2 )
	
	self.Logo:StretchToParent( 2, 2, self.TopPanel:GetWide() * 0.75, 2 )
	self.Logo:SetWide( 128 )
	
	self.SName:SizeToContents()
	self.SName:MoveRightOf( self.Logo, 5 )
	self.SName:Center()
	
	self.InfoPanel:MoveBelow( self.TopPanel, 2 )
	self.InfoPanel:AlignLeft( 4 )
	self.InfoPanel:SetSize( self:GetWide() - 8, 30 )
	
	self.NLabel:SizeToContents()
	self.NLabel:Center()
	self.NLabel:AlignLeft( 8 )
	
	self.RLabel:SizeToContents()
	self.RLabel:Center()
	self.RLabel:AlignLeft( self:GetWide() * 0.35 )
	
	self.TLabel:SizeToContents()
	self.TLabel:Center()
	self.TLabel:AlignLeft( self:GetWide() * 0.5 )
	
	self.FLabel:SizeToContents()
	self.FLabel:Center()
	self.FLabel:AlignLeft( self:GetWide() * 0.85 )
	
	self.PLabel:SizeToContents()
	self.PLabel:Center()
	self.PLabel:AlignLeft( self:GetWide() * 0.95 )
	
	self.PlayerList:MoveBelow( self.InfoPanel, 2 )
	self.PlayerList:AlignLeft( 4 )
	self.PlayerList:SetSize( self:GetWide() - 8, (self:GetTall() * 0.8) - 48 )
	
	for k,v in pairs( self.PlyTable ) do
		v.NAME:AlignLeft( 8 )
		v.RNK:AlignLeft( self:GetWide() * 0.35 )
		v.TIME:AlignLeft( self:GetWide() * 0.5 )
		v.FRAGS:AlignLeft( self:GetWide() * 0.85 )
		v.PING:AlignLeft( self:GetWide() * 0.95 )
	end
	
	//Panel positioning here

end

function PANEL:Paint()
	//Do Painting here
	//self.BaseClass.Paint( self )
	
	draw.RoundedBox( 12, 0, 0, self:GetWide(), self:GetTall(), Color( 10, 10, 10, 255 ) )
	
	function self.TopPanel:Paint()
		surface.SetDrawColor( Color( 45, 45, 45, 255 ) )
		surface.DrawRect( 0, 0, self:GetWide(), self:GetTall() )
		
		surface.SetDrawColor( Color( 90, 90, 90, 255 ) )
		surface.DrawOutlinedRect( 0, 0, self:GetWide(), self:GetTall() )
	end
	
	function self.InfoPanel:Paint()
		surface.SetDrawColor( Color( 200, 200, 200, 255 ) )
		surface.DrawRect( 0, 0, self:GetWide(), self:GetTall() )
		
		surface.SetDrawColor( Color( 180, 180, 180, 255 ) )
		surface.DrawOutlinedRect( 0, 0, self:GetWide(), self:GetTall() )
	end
	
	function self.PlayerList:Paint()
		draw.RoundedBox( 4, 0, 0, self:GetWide(), self:GetTall(), Color( 80, 80, 80, 255 ) )
	end

end

function PANEL:PaintOver()

	//Paint over self and child elements here.

end

function PANEL:Think()
	//Think stuff here
	
	RunConsoleCommand( "xqam_SendTimes" )

end

function TMSG_hook( um )
	TimesT = string.Explode(":", um:ReadString())
end
usermessage.Hook( "TMSG", TMSG_hook )

function PANEL:OnCursorEntered()

	//Do stuff when the cursor enters the panel

end

function PANEL:OnCursorExited()

	//Do Stuff when the cursor leaves the panel

end

function PANEL:OnMouseWheeled()

	//Do stuff when you wheel the mouse on this panel

end

function PANEL:OnMousePressed()

	//Do stuff when you press the mouse on this panel

end

function PANEL:OnMouseReleased()

	//Do stuff when you release the mouse on this panel

end

function PANEL:OnKeyCodePressed()

	//Do stuff when using keys from the keyboard on this panel. This hook uses the keycode enumerations
	//http://wiki.garrysmod.com/?title=P.OnKeyCodePressed	For more information

end

function PANEL:ActionSignal()

	//This is called on the parent frame when a child VGUI element is used. The key corresponds to what is set by Panel.SetCommand.
	//From the wiki
	
end

function PANEL:OnCursorMoved()

	//From the wiki
	//Called whenever the the mouse is moved within the panel.

end

function PANEL:DoClick()

	//For button type panels, use this. Called when you click the button.

end


//Register syntax - Desired element name, Panel to use, base class of the new element.
vgui.Register( "SBoard", PANEL, "DFrame" )


function ColorChatMessages( ply, text, teamchat, alive )
	local chatColour = ColorTable[ply:RANK()]

	if string.Left( text, 1 ) == "!" then return true end
	
	chat.AddText( chatColour, ply:Name() .. ": ", color_white, text )
	return true
end
hook.Add( "OnPlayerChat", "ColorChatMessages", ColorChatMessages )

function ColorTargetIDs()
timer.Simple( 1, function()		//Have to make this a timer because it must overwrite the regular function, AFTER it sets it.
function GAMEMODE:HUDDrawTargetID()
	if CanScoreboard then
		local ENT = LocalPlayer():GetEyeTrace().Entity
		if ENT and ENT:IsValid() and ENT:IsPlayer() and INFOPANEL == nil then
			local RankColor = ColorTable[ENT:RANK()]
			
			INFOPANEL = vgui.Create( "DPanel" )
				INFOPANEL:SetSize( 300, 100 )
				INFOPANEL:SizeToContents()
				INFOPANEL:Center()
				INAME = ENT:Nick()
				function INFOPANEL:Paint()
					return false
				end
			
			local NLabel = vgui.Create( "DLabel", INFOPANEL )
				NLabel:SetTextColor( RankColor )
				NLabel:SetFont( "BoardPlayerNameBig" )
				NLabel:SetText( ENT:Nick() )
				NLabel:SizeToContents()
				NLabel:Center()
				NLabel:AlignTop( 10 )
		
			local HPLabel = vgui.Create( "DLabel", INFOPANEL )
				HPLabel:SetTextColor( RankColor )
				HPLabel:SetFont( "BoardPlayerNameBig" )
				HPLabel:SetText( "Health: " .. ENT:Health() )
				HPLabel:SizeToContents()
				HPLabel:Center()
				HPLabel:MoveBelow( NLabel, 10 )
				
			//INFOPANEL:SizeToContents()
			
			//Draw the custom vgui here, do this because I want to add GivePoints to this
		elseif INFOPANEL != nil and (!ENT or !ENT:IsValid() or !ENT:IsPlayer()) then
			INFOPANEL:Remove()
			INFOPANEL = nil
		elseif INFOPANEL != nil and INAME != ENT:Nick() then
			INFOPANEL:Remove()
			INFOPANEL = nil
		end
	end
	return false
end
end)
end
hook.Add( "Initialize", "ColorTargetIDs", ColorTargetIDs )